<?php
class ServiceOrderRowStatesController extends AppController {
	public $helpers = array('Html', 'Form', 'Session');
	public $components = array('Session');
	public $uses = array('ServiceOrderRowState','ServiceOrderRow', 'State');

//service_order_row_states
	public function index() {
		$this->set('service_order_rows', $this->ServiceOrderRowState->find('all'));
	}

	public function ver() {
	// últimos posts (para el elemento latest_posts)
  // Si la petición fue realizada por medio de requestAction
    if(isset($this->params['requested'])) { 
      return $this->ServiceOrderRowState->find('all');
		}
	}
  

	public function add($id = null) {
		$this->set('service_order_row_id', $id);
		$currentState = $this->ServiceOrderRowState->find('first', array('conditions' => array('ServiceOrderRowState.service_order_row_id' => $id), 'order' => array('ServiceOrderRowState.id DESC')));
		$this->set('next_state',  $this->State->find('first', array('conditions' => array('State.orden' => $currentState['State']['orden'] + 1))));
		$this->set('default_date',date ('Y-m-d H:i:s'));
		//$this->set('next_state', $currentState['State']['orden']+1  );
		$this->set('service_order_row_states', $this->State->find('list', array('fields' => array('id', 'nombre'),'order' => array('State.orden ASC') )));
		if ($this->request->is('post')) {
			$this->ServiceOrderRowState->create();
			if ($this->ServiceOrderRowState->save($this->request->data)) {
				$this->Session->setFlash('Se han guardado los cambios.');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash('No se pudieron guardar los cambios.');
			}
		}
	}
	
	

	public function edit($id = null)  {
		$this->ServiceOrderRowState->id = $id;
		if ($this->request->is('get')) {
			$this->request->data = $this->ServiceOrderRowState->read();
		} else {
			if ($this->ServiceOrderRowState->save($this->request->data)) {
				$this->Session->setFlash('Datos actualizados correctamente.');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash('No se pudieron guardar los cambios.');
			}
		}
	}

}